package uk.co.hs.web.controller.admin;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import uk.co.hs.service.admin.interfaces.WebUserServiceInterface;
import uk.co.hs.web.interceptor.TabsAccessibilityInterceptor;
import uk.co.hs.web.security.authentication.UtilisoftAuthentication;
import uk.co.hs.web.util.SessionTabsAccessibility;

@Controller("hs_userListController")
@RequestMapping("/" + TabsAccessibilityInterceptor.USER_LIST_URL)
public class UserListController extends AdminListController
{
  @Autowired(required=true)
  @Qualifier("hs_webUserService")
  public void setAdminService(WebUserServiceInterface aWebUserService)
  {
    mAdminService = aWebUserService;
  }

  @RequestMapping(method = RequestMethod.GET)
  public ModelAndView formBackingObject(HttpServletRequest aRequest) throws Exception
  {
    SessionTabsAccessibility tabsAccessibility = (SessionTabsAccessibility) aRequest.getSession().getAttribute(TabsAccessibilityInterceptor.TABS_ACCESSIBILITY);
    if (!tabsAccessibility.getUserEditAccess())
    {
      return super.formBackingObject(aRequest, Boolean.FALSE, Boolean.FALSE);
    }

    UtilisoftAuthentication auth = (UtilisoftAuthentication) SecurityContextHolder.getContext().getAuthentication();
    if (!auth.getAllowedURLS().get(TabsAccessibilityInterceptor.USER_EDIT_URL))
    {
      return super.formBackingObject(aRequest, Boolean.FALSE, Boolean.TRUE);
    }
    return super.formBackingObject(aRequest, Boolean.TRUE, Boolean.TRUE);
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setControllerName()
   * {@inheritDoc}
   */
  public void setControllerName()
  {
    mControllerName = "User";
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setObjectName()
   * {@inheritDoc}
   */
  public void setObjectName()
  {
    mObjectName = "User";
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setColumnMessageCodes()
   * {@inheritDoc}
   */
  public void setColumnMessageCodes()
  {
    mColumnMessageCodes = new ArrayList<Object>();
    mColumnMessageCodes.add("field.name");
    mColumnMessageCodes.add("field.role");
    mColumnMessageCodes.add("field.active");
    mColumnMessageCodes.add("field.unlocked");
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setColumnWidths()
   * {@inheritDoc}
   */
  public void setColumnWidths()
  {
    mColumnWidths = new ArrayList<Object>();
    mColumnWidths.add(200);
    mColumnWidths.add(200);
    mColumnWidths.add(100);
    mColumnWidths.add(100);
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setNumberOfIdentifiers()
   * {@inheritDoc}
   */
  public void setNumberOfIdentifiers()
  {
    mNumberOfIdentifiers = 1;
  }

  /**
   * @see uk.co.hs.web.controller.admin.AdminListController
   * #setViewName()
   * {@inheritDoc}
   */
  public void setViewName()
  {
    mViewName = "hs_UserList";
  }
}
